package com.test.todo;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.test.todo.databse.TodoDbAdapter;
import com.test.todo.model.Todo;

public class TodoDetails extends Activity {

    private EditText mTitleText;
    private EditText mBodyText;
    private Long mRowId;
    private TodoDbAdapter mDbHelper;
    private Spinner mCategory;
    private Todo mTodo = null;

    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        mDbHelper = new TodoDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.todo_edit);
        mCategory = (Spinner) findViewById(R.id.category);
        mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
        mBodyText = (EditText) findViewById(R.id.todo_edit_description);

        Button confirmButton = (Button) findViewById(R.id.todo_edit_button);
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        mRowId = (bundle == null) ? null : (Long) bundle.getSerializable(TodoDbAdapter.KEY_ROWID);
        if (extras != null) {
            mRowId = extras.getLong(TodoDbAdapter.KEY_ROWID);
        }
        populateFields();
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                setResult(RESULT_OK);
                finish();
            }

        });
    }

    private void populateFields() {

        if (mRowId != null) {
            Cursor todo = mDbHelper.fetchTodo(mRowId);
            startManagingCursor(todo);
            String category = todo.getString(todo.getColumnIndexOrThrow(TodoDbAdapter.KEY_CATEGORY));

            for (int i = 0; i < mCategory.getCount(); i++) {

                String s = (String) mCategory.getItemAtPosition(i);
                Log.e(null, s + " " + category);
                if (s.equalsIgnoreCase(category)) {
                    mCategory.setSelection(i);
                }
            }

            mTitleText.setText(todo.getString(todo.getColumnIndexOrThrow(TodoDbAdapter.KEY_SUMMARY)));
            mBodyText.setText(todo.getString(todo.getColumnIndexOrThrow(TodoDbAdapter.KEY_DESCRIPTION)));
        }
    }

    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(TodoDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {

        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {

        super.onResume();
        populateFields();
    }

    private void saveState() {

        mTodo = new Todo();
        mTodo.category = (String) mCategory.getSelectedItem();
        mTodo.summary = mTitleText.getText().toString();
        mTodo.description = mBodyText.getText().toString();

        if (mRowId == null) {
            long id = mDbHelper.createTodo(mTodo);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateTodo(mRowId, mTodo);
        }
    }
}
